home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / satellit / vstsrc / sitedb.c < prev    next >
C/C++ Source or Header  |  1995-01-28  |  9KB  |  235 lines

  1. /*
  2.  * %W% %E% %U%  [EXTREL_1.2]
  3.  *
  4.  * VersaTrack orbit calculations are based on those that appear in Dr. Manfred
  5.  * Bester's sattrack program (the Unix(tm) versions 1 and 2).
  6.  *
  7.  * The data from which the maps where generated come from "xsat", an
  8.  * X-Windows program by David A. Curry (N9MSW).
  9.  *
  10.  * Site coordinates come from various sources, including a couple of
  11.  * World Almanacs, and also from both of the programs mentioned above.
  12.  *
  13.  * The following are authors' applicable copyright notices:
  14.  *
  15.  *                                                                               
  16.  * Copyright (c) 1992, 1993, 1994 Manfred Bester. All Rights Reserved.        
  17.  *                                                                           
  18.  * Permission to use, copy, modify, and distribute this software and its      
  19.  * documentation for educational, research and non-profit purposes, without   
  20.  * fee, and without a written agreement is hereby granted, provided that the  
  21.  * above copyright notice and the following three paragraphs appear in all    
  22.  * copies.                                                                    
  23.  *                                                                              
  24.  * Permission to incorporate this software into commercial products may be    
  25.  * obtained from the author, Dr. Manfred Bester, 1636 M. L. King Jr. Way,     
  26.  * Berkeley, CA 94709, USA.                                                   
  27.  *                                                                             
  28.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,  
  29.  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF    
  30.  * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED   
  31.  * OF THE POSSIBILITY OF SUCH DAMAGE.                                         
  32.  *                                                                             
  33.  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT       
  34.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A    
  35.  * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"       
  36.  * BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,  
  37.  * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.                                   
  38.  *                                                                             
  39.  *                                                                             
  40.  * Copyright 1992 by David A. Curry                                            
  41.  *                                                                             
  42.  * Permission to use, copy, modify, distribute, and sell this software and its 
  43.  * documentation for any purpose is hereby granted without fee, provided that  
  44.  * the above copyright notice appear in all copies and that both that copyright
  45.  * notice and this permission notice appear in supporting documentation.  The  
  46.  * author makes no representations about the suitability of this software for  
  47.  * any purpose.  It is provided "as is" without express or implied warranty.   
  48.  *                                                                             
  49.  * David A. Curry, N9MSW                                                       
  50.  * Purdue University                                                           
  51.  * Engineering Computer Network                                                
  52.  * 1285 Electrical Engineering Building                                        
  53.  * West Lafayette, IN 47907                                                    
  54.  * davy@ecn.purdue.edu                                                         
  55.  *                                                                             
  56.  * VersaTrack Copyright (c) 1993, 1994 Siamack Navabpour. All Rights Reserved.
  57.  *
  58.  * Permission is hereby granted to copy, modify and distribute VersaTrack
  59.  * in whole, or in part, for educational, non-profit and non-commercial use
  60.  * only, free of charge or obligation, and without agreement, provided that
  61.  * all copyrights and restrictions noted herein are observed and followed, and
  62.  * additionally, that this and all other copyright notices listed herein
  63.  * appear unaltered in all copies and in all derived work.
  64.  *
  65.  * This notice shall not in any way void or supersede any of the other authors
  66.  * rights or privileges.
  67.  *
  68.  * VersaTrack IS PRESENTED FREE AND "AS IS", WITHOUT ANY WARRANTY OR SUPPORT.
  69.  * YOU USE IT AT YOUR OWN RISK. The author(s) shall not be liable for any
  70.  * direct, indirect, incidental, or consequential damage, loss of profits or
  71.  * other tangible or intangible losses or benefits, arising out of or related
  72.  * to its use. VersaTrack carries no warranty, explicit or implied, including
  73.  * but not limited to those of merchantablity and fitness for a particular
  74.  * purpose.
  75.  *
  76.  * Siamack Navabpour, 12342 Hunter's Chase Dr. Apt. 2114, Austin, TX 78729.
  77.  * sia@bga.com or sia@realtime.com.
  78.  */
  79.  
  80.  
  81. #include <windows.h>
  82. #include <stdio.h>
  83. #include <math.h>
  84. #include <stdlib.h>
  85.  
  86. #include "vstdefs.h"
  87. #include "constant.h"
  88. #include "vsttype.h"
  89. #include "libxtrns.h"
  90.  
  91.  
  92. static site_t *siteinfop; 
  93. static char *memmsg = "Can't allocate memory";
  94. /*
  95.  * Read the site database, sort and return address of list of ptrs to site_t's.
  96.  */
  97. site_t **
  98. readSiteDB(int version, char *dirp, char *sitefile, unsigned char *ES, char *tbuf)
  99. {
  100.     char c;
  101.     FILE *fp;
  102.     HANDLE hg;
  103.     int d, m, s, index;
  104.     site_t *ci, **cii;
  105.     char linebuf[180], *line;
  106.     char *city, *stcntry, *lat, *lon, *elev, *stz, *tzstr;
  107.     double sinLat, cosLat, sinLong, cosLong, now;
  108.     extern int sitecmp(const void *, const void *);
  109.     
  110.     sprintf(linebuf,"%s\\%s", dirp, sitefile);
  111.     if ((fp = fopen(linebuf, "r")) == NULL) {
  112.         sprintf(tbuf,"Can't open %s", linebuf);
  113.         return NULL;
  114.     }
  115.  
  116.     index = 0;
  117.     now = utctime();
  118.     while ((line = getline(linebuf, sizeof(linebuf), fp)) != NULL) {
  119.         if ((*line == '#') || (*line == '\n'))
  120.             continue;
  121.  
  122.         city = strtok(line, ",");
  123.         stripTrailingSpace(city);
  124.                 
  125.         stcntry = strtok(NULL, ";");
  126.         if (stcntry == NULL)
  127.             stcntry = "";
  128.  
  129.         stripTrailingSpace(stcntry);
  130.         stcntry = stripLeadingSpace(stcntry);
  131.  
  132.         if (strlen(city) > 31)
  133.             city[31] = 0;
  134.         if (strlen(stcntry) > 39)
  135.             stcntry[39] = 0;
  136.  
  137.         lat = strtok(NULL, ";");
  138.         lon = strtok(NULL, ";");
  139.         elev = strtok(NULL, ";");
  140.         stz = strtok(NULL, "; ");
  141.         tzstr = strtok(NULL, "; ");
  142.  
  143.         if (!(hg = GlobalAlloc(GPTR, sizeof(site_t)))) {
  144.             strcpy(tbuf,memmsg);
  145.             return NULL;
  146.         }
  147.         if (siteinfop == NULL) {
  148.             siteinfop = (site_t *) GlobalLock(hg);
  149.             ci = siteinfop;
  150.         }
  151.         else {
  152.             ci->c_next = (site_t *) GlobalLock(hg);
  153.             ci = ci->c_next;
  154.         }
  155.         if (tzstr) {
  156.             tzstr = stripLeadingSpace(tzstr);
  157.                 strncpy(ci->c_tzname, tzstr, sizeof(ci->c_tzname));
  158.            }
  159.            else
  160.                strcpy(ci->c_tzname, "UTC");
  161.  
  162.         ci->c_lbid = index++;
  163.         ci->c_next = NULL;
  164.                 
  165.         if ((ci->c_name = saveString(city ? city : "?")) == NULL) {
  166.             strcpy(tbuf, memmsg);
  167.             return NULL;
  168.         }
  169.  
  170.         if ((ci->c_locale = saveString(stcntry ? stcntry : "?")) == NULL) {
  171.             strcpy(tbuf, memmsg);
  172.             return NULL;
  173.         }
  174.  
  175.         c = 0;
  176.         sscanf(lat ? lat : "00 00 00 N", "%d %d %d %c", &d, &m, &s, &c);
  177.  
  178.         ci->c_lat = (double)d + (double) m / 60.0 + (double) s / 3600.0;
  179.  
  180.         if ((c == 'S') || (c == 's'))
  181.             ci->c_lat = 0.0 - ci->c_lat;
  182.  
  183.         sscanf(lon ? lon : "00 00 00 W", "%d %d %d %c", &d, &m, &s, &c);
  184.  
  185.         ci->c_lng = (double) d + (double) m / 60 + (double) s / 3600.0;
  186.  
  187.         if ((c == 'E') || (c == 'e'))
  188.             ci->c_lng = 0.0 - ci->c_lng;
  189.  
  190.         ci->c_alt = atof(elev ? elev : "0.0") * CMKM;
  191.  
  192.            ci->c_timezone = atof(stz ? stz : "0.0") / 24.0;                
  193.         ci->c_lat   *= CDR;
  194.         ci->c_lng   *= CDR;
  195.  
  196.         cosLat  = cos(ci->c_lat);
  197.         sinLat  = sin(ci->c_lat);
  198.         cosLong = cos(ci->c_lng);
  199.         sinLong = sin(ci->c_lng);
  200.  
  201.         ci->c_vecX = ci->c_alt * cosLong * cosLat;
  202.         ci->c_vecY = ci->c_alt * sinLong * cosLat;
  203.         ci->c_vecZ = ci->c_alt * sinLat;
  204.  
  205.            if (hg)
  206.             GlobalUnlock(hg);
  207.     }
  208.     fclose(fp);
  209.  
  210.     if ((hg = GlobalAlloc(GPTR, sizeof(site_t **) * (index+1))) == NULL)
  211.         return NULL; /* need a message here */
  212.  
  213.     cii = (site_t **) GlobalLock(hg);
  214.  
  215.     for (index=0, ci=siteinfop; ci; ci = ci->c_next)
  216.         cii[index++] = ci;
  217.     cii[index] = 0;
  218.  
  219.     qsort(cii, index, sizeof(site_t *), sitecmp);
  220.  
  221.     return cii;
  222. }
  223.  
  224. static int
  225. sitecmp(const void *a, const void *b)
  226. {
  227.     char abuf[80], bbuf[80];
  228.  
  229.     strncpy(abuf,(*(site_t **)a)->c_name, 39);
  230.     strncat(abuf,(*(site_t **)a)->c_locale, 40);
  231.     strncpy(bbuf,(*(site_t **)b)->c_name, 39);
  232.     strncat(bbuf,(*(site_t **)b)->c_locale, 40);
  233.     return cistrcmp(abuf, bbuf);
  234. }
  235.